package com.runmi.webchat.aftersale.web.common.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.web.base.BaseController;


@Controller
@RequestMapping(value = "/common/img")
public class ImageController  extends BaseController{
	
	 @RequestMapping(value = "/show")
	 public void   show(HttpServletRequest request,HttpServletResponse response,String imageName,String dispName){
		 String fileUrl=Constants.SYSTEM_IMAGE_FILE_PATH+"//"+imageName;  
	        try {  
	        	String imageType="png";
	            File filePath = new File(fileUrl);  
	            if(!filePath.exists()){
	            	return;
	            }
	            String fileName=filePath.getName();
	            String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
	            if(filePath.exists()){  
	                //读图片  
	                FileInputStream inputStream = new FileInputStream(filePath);    
	                int available = inputStream.available();  
	                byte[] data = new byte[available];  
	                inputStream.read(data);    
	                inputStream.close();    
	                //写图片  
	                response.setContentType("image/"+prefix);    
	                response.setCharacterEncoding("UTF-8");  
//	                String agent = request.getHeader("USER-AGENT");
//	                String outFileName=dispName +"." + prefix;
//	                if(agent != null && agent.toLowerCase().contains("firefox")) {
//	                	outFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64((outFileName ).getBytes("UTF-8")))) + "?=";
//	                }else {
//	                	outFileName = URLEncoder.encode(outFileName, "UTF-8");
//	                }
//	                response.setHeader("Content-disposition","attachment; filename=" + outFileName );
	                OutputStream stream = new BufferedOutputStream(response.getOutputStream());    
	                stream.write(data);    
	                stream.flush();    
	                stream.close();    
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } 

	 }

}
